#!/usr/bin/env python2
# vim: set ts=4 sw=4 fileencoding=utf-8:
# Luomio <nohappiness@gmail.com>
# Filename: dde-first-run.py
# Create Date: 27-03, 13

from os import path, rename, remove
import locale
import subprocess
import os
import shutil
import json
import math

def system_screenshot_isA():
    try:
        import gobject
        from dbus import glib
        import dbus
    except ImportError:
        return False

    gobject.threads_init()
    glib.init_threads()
    bus = dbus.SessionBus()
    keybinding = bus.get_object("com.deepin.daemon.Keybinding", "/com/deepin/daemon/Keybinding")

    obj = json.loads(keybinding.List())
    for i in xrange(len(obj)):
        if obj[i]['Id'] == "screenshot":
            if '<Control><Alt>A' in obj[i]['Accels']:
                return True

    return False

def add_qq_shortcut(_name, _key, _short_cut):
    _action = "/opt/deepinwine/tools/sendkeys.sh"
    if path.exists(_action):
        try:
            import gobject
            from dbus import glib
            import dbus
        except ImportError:
            return False

        gobject.threads_init()
        glib.init_threads()
        bus = dbus.SessionBus()
        keybinding = bus.get_object("com.deepin.daemon.Keybinding", "/com/deepin/daemon/Keybinding")

        obj = json.loads(keybinding.List())
        for i in xrange(len(obj)):
            if obj[i]['Name'] == _name:
                keybinding.Delete(obj[i]['Id'], obj[i]['Type'])

        _, ok = keybinding.Add(_name, _action+_key, _short_cut)
        if ok == False:
            return False

if __name__ == "__main__":
    import sys

    if len(sys.argv) < 4:
        add_qq_shortcut(u"提取QQ消息", " z", "<Control><Alt>Z")
        if system_screenshot_isA():
            add_qq_shortcut(u"QQ截图", " a", "<Control><Alt>S")
        else:
            add_qq_shortcut(u"QQ截图", " a", "<Control><Alt>A")
        sys.exit(0)

    add_qq_shortcut(unicode(sys.argv[1], 'utf-8'), sys.argv[2], sys.argv[3])
